package com.qiyi.components.preview.container
{
   import flash.display.Sprite;
   import flash.geom.Rectangle;
   import flash.text.TextField;
   import flash.display.Shape;
   import com.qiyi.player.base.pub.EnumItem;
   import com.qiyi.player.core.player.IPlayer;
   import flash.text.TextFormat;
   import com.qiyi.player.wonder.common.ui.FastCreator;
   import flash.events.MouseEvent;
   import com.qiyi.components.preview.events.PreviewEvent;
   import flash.events.Event;
   import flash.utils.clearTimeout;
   import com.qiyi.player.core.model.def.StreamEnum;
   import gs.TweenLite;
   import flash.utils.setTimeout;
   import com.qiyi.components.preview.media.PrevewVideo;
   
   public class Preview extends Sprite
   {
      
      public function Preview() {
         super();
         this.initUI();
         this.initListener();
      }
      
      public static const DELAY_PREVIEW_VIDEO:uint = 1000;
      
      public static const TAIL_WID:uint = 6;
      
      public static const TAIL_HEI:uint = 6;
      
      public static const SPOT_WID:uint = 112;
      
      public static const SPOT_HEI:uint = 38;
      
      public static const RECT_0:Rectangle = new Rectangle(0,0,60,18);
      
      public static const RECT_1:Rectangle = new Rectangle(0,0,160,90);
      
      public static const RECT_2:Rectangle = new Rectangle(0,0,190,106);
      
      public static const RECT_3:Rectangle = new Rectangle(0,0,160,33);
      
      private var spotTxt:TextField;
      
      private var ps:PreviewSprite;
      
      private var tail:Shape;
      
      private var spotInter:int;
      
      private var timeInter:int;
      
      private var _type:int = 0;
      
      private var _time:Number = 0;
      
      private var _enabled:Boolean = true;
      
      private var _clkAction:Boolean = true;
      
      private var _streamType:EnumItem;
      
      public function setModel(param1:IPlayer) : void {
         this.ps.player.setModel(param1);
         this._streamType = param1.movieModel.streamType;
      }
      
      private function initUI() : void {
         this.ps = new PreviewSprite();
         this.ps.y = -TAIL_HEI;
         addChild(this.ps);
         this.spotTxt = new TextField();
         this.spotTxt.defaultTextFormat = new TextFormat(FastCreator.FONT_MSYH,12,11579568,false,null,null,null,null,"center");
         this.spotTxt.multiline = true;
         this.spotTxt.wordWrap = true;
         this.spotTxt.width = SPOT_WID;
         this.spotTxt.height = SPOT_HEI;
         this.spotTxt.x = -SPOT_WID / 2;
         this.spotTxt.y = -SPOT_HEI - TAIL_HEI - RECT_0.height - 8;
         this.spotTxt.mouseEnabled = false;
         addChild(this.spotTxt);
         this.tail = new Shape();
         this.tail.graphics.clear();
         this.tail.graphics.beginFill(16777215,0);
         this.tail.graphics.moveTo(-6,0);
         this.tail.graphics.lineTo(6,0);
         this.tail.graphics.lineTo(RECT_1.width / 2 - 20,-TAIL_HEI);
         this.tail.graphics.lineTo(-RECT_1.width / 2 + 20,-TAIL_HEI);
         this.tail.graphics.lineTo(-6,0);
         this.tail.graphics.endFill();
         this.tail.graphics.lineStyle(1,0);
         this.tail.graphics.beginFill(0,1);
         this.tail.graphics.moveTo(-TAIL_WID / 2,-TAIL_HEI);
         this.tail.graphics.lineTo(TAIL_WID / 2,-TAIL_HEI);
         this.tail.graphics.lineStyle(1,3355443);
         this.tail.graphics.lineTo(0,0);
         this.tail.graphics.lineTo(-TAIL_WID / 2,-TAIL_HEI);
         this.tail.graphics.endFill();
         addChild(this.tail);
         this.spotTxt.visible = false;
      }
      
      private function initListener() : void {
         this.ps.addEventListener(MouseEvent.CLICK,this.onClk);
         this.ps.addEventListener(MouseEvent.ROLL_OVER,this.onRollOver);
         this.ps.addEventListener(PreviewEvent.SEEKBAR_ROLL_OUT,this.onBarRollOut);
         this.ps.addEventListener(PreviewEvent.PREVIEW_ALL_STAGE,this.onPreviewAllStage);
      }
      
      private function onRemoved(param1:Event) : void {
         this.ps.removeEventListener(MouseEvent.CLICK,this.onClk);
         this.ps.removeEventListener(MouseEvent.ROLL_OVER,this.onRollOver);
         this.ps.removeEventListener(PreviewEvent.SEEKBAR_ROLL_OUT,this.onBarRollOut);
         this.ps.removeEventListener(PreviewEvent.PREVIEW_ALL_STAGE,this.onPreviewAllStage);
      }
      
      private function onRollOver(param1:MouseEvent) : void {
         if(this.ps.hasEventListener(MouseEvent.ROLL_OUT))
         {
            clearTimeout(this.timeInter);
            clearTimeout(this.spotInter);
            if(this._streamType == StreamEnum.RTMP)
            {
               return;
            }
            if(this._type == 0)
            {
               this.ps.bigSize();
            }
            else if(this._type == 1)
            {
               TweenLite.to(this.spotTxt,0.2,{"y":-SPOT_HEI - TAIL_HEI});
               this.ps.spotBigSize();
            }
            
         }
         else
         {
            this.visible = false;
         }
      }
      
      private function onRollOut(param1:MouseEvent) : void {
         if(this._streamType == StreamEnum.RTMP)
         {
            return;
         }
         if(this._type == 0)
         {
            this.ps.middleSize();
         }
         else if(this._type == 1)
         {
            this.ps.spotMiddleSize();
         }
         
      }
      
      private function onClk(param1:MouseEvent) : void {
         this.ps.removeEventListener(MouseEvent.CLICK,this.onClk);
         this.spotTxt.visible = false;
         this.tail.visible = false;
         var _loc2_:Number = stage.mouseY;
         if(this._streamType == StreamEnum.RTMP || !this._clkAction)
         {
            return;
         }
         this.ps.previewAllStage(this.x,_loc2_);
      }
      
      private function onBarRollOut(param1:PreviewEvent) : void {
         dispatchEvent(new PreviewEvent(PreviewEvent.SEEKBAR_ROLL_OUT));
      }
      
      private function onPreviewAllStage(param1:PreviewEvent) : void {
         visible = false;
         this.hidePreview();
         dispatchEvent(new PreviewEvent(PreviewEvent.SEEKBAR_ROLL_OUT));
      }
      
      private function setStatus(param1:int) : void {
         this.tail.visible = true;
         this._type = param1;
         switch(param1)
         {
            case 0:
               this.spotTxt.visible = false;
               this.ps.y = -TAIL_HEI;
               break;
            case 1:
               this.spotTxt.visible = true;
               this.spotTxt.y = -SPOT_HEI - TAIL_HEI - RECT_0.height;
               this.ps.y = -TAIL_HEI;
               break;
         }
      }
      
      private function spotInterDelay() : void {
         this.ps.previewSpotVideo();
         this.ps.addEventListener(MouseEvent.ROLL_OUT,this.onRollOut);
         TweenLite.to(this.spotTxt,0.2,{"y":-SPOT_HEI - TAIL_HEI});
         dispatchEvent(new PreviewEvent(PreviewEvent.DISABLE_MOUSE_MOVE));
         dispatchEvent(new PreviewEvent(PreviewEvent.PREVIEW_PINGBAK,false,false,this._time));
      }
      
      private function timeInterDelay() : void {
         this.ps.previewVideo();
         this.ps.addEventListener(MouseEvent.ROLL_OUT,this.onRollOut);
         dispatchEvent(new PreviewEvent(PreviewEvent.DISABLE_MOUSE_MOVE));
         dispatchEvent(new PreviewEvent(PreviewEvent.PREVIEW_PINGBAK,false,false,this._time));
      }
      
      private function set mouseUse(param1:Boolean) : void {
         if(param1)
         {
            this.mouseChildren = true;
            this.mouseEnabled = true;
         }
         else
         {
            this.mouseChildren = false;
            this.mouseEnabled = false;
         }
      }
      
      public function get time() : Number {
         return this._time;
      }
      
      public function previewSpots(param1:String, param2:String, param3:Number) : void {
         if(this.x > this.ps.realWidth)
         {
            this.hidePreview();
         }
         this.visible = true;
         this.ps.removeEventListener(MouseEvent.ROLL_OUT,this.onRollOut);
         this.setStatus(1);
         this.spotTxt.text = param1;
         this._time = param3;
         this.ps.previewSpot(param2,param3,this.x);
         TweenLite.killTweensOf(this.spotTxt);
         clearTimeout(this.spotInter);
         clearTimeout(this.timeInter);
         if(this._streamType == StreamEnum.HTTP && (this._enabled))
         {
            this.spotInter = setTimeout(this.spotInterDelay,DELAY_PREVIEW_VIDEO);
         }
         if(this.ps.hasEventListener(MouseEvent.CLICK) == false)
         {
            this.ps.addEventListener(MouseEvent.CLICK,this.onClk);
         }
      }
      
      public function previewTime(param1:String, param2:Number) : void {
         if(this.x > this.ps.realWidth)
         {
            this.hidePreview();
         }
         this.visible = true;
         this.ps.removeEventListener(MouseEvent.ROLL_OUT,this.onRollOut);
         this.setStatus(0);
         this._time = param2;
         this.ps.previewTime(param1,param2,this.x);
         clearTimeout(this.timeInter);
         clearTimeout(this.spotInter);
         TweenLite.killTweensOf(this.spotTxt);
         if(this._streamType == StreamEnum.HTTP && (this._enabled))
         {
            this.timeInter = setTimeout(this.timeInterDelay,DELAY_PREVIEW_VIDEO);
         }
         if(this.ps.hasEventListener(MouseEvent.CLICK) == false)
         {
            this.ps.addEventListener(MouseEvent.CLICK,this.onClk);
         }
      }
      
      public function hidePreview() : void {
         this._type = -1;
         this.spotTxt.visible = false;
         this.ps.hidePrivew();
         TweenLite.killTweensOf(this.spotTxt);
         clearTimeout(this.spotInter);
         clearTimeout(this.timeInter);
      }
      
      public function setSpotOffset(param1:Number) : void {
         this.spotTxt.x = -SPOT_WID / 2 + param1;
      }
      
      public function set enabled(param1:Boolean) : void {
         this._enabled = param1;
      }
      
      public function set gap(param1:Number) : void {
         if((isNaN(param1)) || param1 < 0)
         {
            throw new Error("Preview set Gap Value Invalid","px_0");
         }
         else
         {
            if(this.ps)
            {
               this.ps.gap = param1;
            }
            return;
         }
      }
      
      public function set realWidth(param1:Number) : void {
         if((isNaN(param1)) || param1 < 0)
         {
            return;
         }
         if(this.ps)
         {
            this.ps.realWidth = param1;
         }
      }
      
      public function set clkAction(param1:Boolean) : void {
         if(!(param1 is Boolean))
         {
            throw new Error("Preview Set clkAction Value Invalid","px_2");
         }
         else
         {
            this._clkAction = param1;
            return;
         }
      }
      
      public function get mediaSprite() : PreviewSprite {
         return this.ps;
      }
      
      public function get media() : PrevewVideo {
         if(this.ps)
         {
            return this.ps.player;
         }
         return null;
      }
      
      public function get enableMoveIn() : Boolean {
         return this.ps.enableMoveIn;
      }
   }
}
